package com.axend.aerosense.user.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.IComplete;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.user.BR;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserEmailFragmentBinding;
import com.axend.aerosense.user.viewmodel.EmailSettingViewModel;

public class UserEmailSettingFragment extends MvvmBaseFragment<UserEmailFragmentBinding, EmailSettingViewModel>
    implements IOperationResult, IComplete {
    private UserInfo userInfo;
    public final static String PARAM_EMAIL = "PARAM_EMAIL";

    public static UserEmailSettingFragment getInstance(){
        UserEmailSettingFragment fragment = new UserEmailSettingFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(PARAM_EMAIL, email);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    protected void initParameters() {
        super.initParameters();
    }

    @Override
    public void onResume() {
        super.onResume();
        ((UserSettingActivity)getActivity()).setTitle(getString(R.string.user_email_set));
        initView();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private void initView() {
        if(userInfo != null){
            viewDataBinding.userEmail.setText(userInfo.userEmail);
        }
        if(!TextUtils.isEmpty(userInfo.userEmail)){
            viewDataBinding.userSave.setVisibility(View.GONE);
            viewModel.getCanEdit().setValue(false);
            ((UserSettingActivity)getActivity()).setRightTitle(getString(R.string.common_edit));

        }else{
            viewDataBinding.userSave.setVisibility(View.VISIBLE);
            viewModel.getCanEdit().setValue(true);
            viewDataBinding.userSave.setOnClickListener(v->{
                viewModel.setEmail(getActivity(), this);
            });
        }
//        viewDataBinding.userSave.setClickable(false);
//        viewModel.getEmail().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(String s) {
//                if(CheckUtil.isEmail(s)){
//                    viewDataBinding.userSave.setClickable(true);
//                }else{
//                    viewDataBinding.userSave.setClickable(false);
//                }
//            }
//        });

    }

    @Override
    public void onDetach() {
        ((UserSettingActivity)getActivity()).setRightTitle("");
        super.onDetach();
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_email_fragment;
    }

    @Override
    public int getBindingVariable() {
        return BR.data;
    }

    @Override
    protected EmailSettingViewModel getViewModel() {
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        if(null!= userInfo &&
                (TextUtils.isEmpty(userInfo.userEmail) || getString(R.string.common_un_set).equals(userInfo.userEmail))){
            userInfo.userEmail = "";
        }
        EmailSettingViewModel tmp = new ViewModelProvider(this).get(EmailSettingViewModel.class);
        tmp.getEmail().setValue(userInfo.userEmail);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onSuccess() {
        getActivity().onBackPressed();
    }

    @Override
    public void onFail() {

    }

    @Override
    public void complete() {
        if(isAdded()) {
            if (!viewModel.getCanEdit().getValue()) {
                viewModel.getCanEdit().setValue(true);
                viewDataBinding.userEmail.setFocusable(true);
                viewDataBinding.userEmail.setFocusableInTouchMode(true);
                viewDataBinding.userEmail.requestFocus();
                ((UserSettingActivity) getActivity()).setRightTitle(getString(R.string.common_save));


            } else {
                viewModel.getCanEdit().setValue(false);
                ((UserSettingActivity) getActivity()).setRightTitle(getString(R.string.common_edit));
                String email = viewModel.getEmail().getValue();
                if(!email.equals(userInfo.userEmail)){
                    viewModel.setEmail(getActivity(), this);
                }
            }
        }
    }
}
